class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def insertIntoMaxTree(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        new_node = TreeNode(val)

        def search(node):
            if not node:
                return new_node
            if node.val < val:
                new_node.left = node
                return new_node
            else:
                node.right = search(node.right)
                return node

        return search(root)
